<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增spu</el-breadcrumb-item>
      <el-breadcrumb-item>第3步: 选择商品相册</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <div style="width: 600px; margin: 0;
  height: 250px">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
        <el-form-item label="商品相册" prop="brandId">
          <el-select v-model="ruleForm.albumId" placeholder="请选择">
            <el-option
                v-for="item in albumListOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="warning" @click="backToPreStep()">上一步</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      albumListOptions: [],
      ruleForm: {
        albumId: '',
        albumName:''
      },
      rules: {
        albumId: [
          {required: true, message: '请输入拼音', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    backToPreStep() {
      this.$router.push('add-new2');
    },
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      if (!localRuleFormString){
        this.$router.push('add-new1');
      }

      let localRuleForm = JSON.parse(localRuleFormString);
      console.log(localRuleForm);
      if (!localRuleForm.categoryId){
        this.$router.push('add-new1');
      }else if (!localRuleForm.brandId){
        this.$router.push('add-new2');
      }

      this.ruleForm = localRuleForm;
    },
    loadAlbumList() {
      let url = 'http://localhost:8080/albums/list';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.albumListOptions = responseBody.data;
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/albums/' + this.ruleForm.albumId+'/select';
          console.log('url = ' + url);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              let album = responseBody.data;
              this.ruleForm.albumId = album.id;
              this.ruleForm.albumName = album.name;
              console.log(this.ruleForm);
              localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
              this.$router.push('add-new4');
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadAlbumList();
    this.loadLocalRuleForm();
  }
}
</script>

<style>
body {
  background: #2c3e50;
}

.el-input {
  width: 1000px;
}
</style>